package com.prism.e4.rcp.ui.parts;

import java.beans.PropertyChangeListener;

import javax.annotation.PostConstruct;

import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;

import swing2swt.layout.BoxLayout;

import org.eclipse.swt.dnd.DragSource;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.DragSourceAdapter;
import org.eclipse.swt.dnd.DragSourceEvent;
import org.eclipse.swt.dnd.DragSourceListener;
import org.eclipse.swt.dnd.DropTarget;
import org.eclipse.swt.dnd.DropTargetAdapter;
import org.eclipse.swt.dnd.DropTargetEvent;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Group;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.gef.*;
import org.eclipse.gef.dnd.TemplateTransferDragSourceListener;
import org.eclipse.gef.editparts.AbstractGraphicalEditPart;
import org.eclipse.gef.palette.PaletteRoot;
import org.eclipse.gef.ui.palette.PaletteViewer;
import org.eclipse.gef.ui.palette.PaletteViewerProvider;
import org.eclipse.gef.ui.parts.GraphicalEditorWithFlyoutPalette;
import org.eclipse.swt.widgets.Text;

public class GraphicalEditor2 extends GraphicalEditorWithFlyoutPalette {

	@PostConstruct
	public void createControls(Composite parent) {
	}

	protected void configureGraphicalViewer() {
		super.configureGraphicalViewer();

		GraphicalViewer viewer = getGraphicalViewer();
		viewer.setEditPartFactory(new SubnetEditPartFactory());
		ScalableFreeformRootEditPart rootEditPart = new ScalableFreeformRootEditPart();
		viewer.setRootEditPart(rootEditPart);
		keyHandler = new GraphicalViewerKeyHandler(viewer);
		viewer.setKeyHandler(keyHandler);

		// TIP: Do everything to enable the zooming.
		ZoomManager manager = rootEditPart.getZoomManager();
		IAction action = new ZoomInAction(manager);
		getActionRegistry().registerAction(action);
		action = new ZoomOutAction(manager);
		getActionRegistry().registerAction(action);
		viewer.setProperty(MouseWheelHandler.KeyGenerator.getKey(SWT.CTRL),
				MouseWheelZoomHandler.SINGLETON);

		// configure the context menu provider
		ContextMenuProvider cmProvider = new NetEditorContextMenuProvider(
				viewer, getActionRegistry());
		viewer.setContextMenu(cmProvider);
		getSite().registerContextMenu(cmProvider, viewer);

		keyHandler.put(KeyStroke.getPressed('\177', 127, 0),
				getActionRegistry().getAction(ActionFactory.DELETE.getId()));
		keyHandler
				.put(KeyStroke.getPressed('a', 0x1, SWT.CTRL),
						getActionRegistry().getAction(
								ActionFactory.SELECT_ALL.getId()));
		keyHandler
				.put(KeyStroke.getPressed('A', 0x1, SWT.CTRL),
						getActionRegistry().getAction(
								ActionFactory.SELECT_ALL.getId()));
		keyHandler.put(KeyStroke.getPressed('c', 0x1, SWT.CTRL),
				getActionRegistry().getAction(ActionFactory.COPY.getId()));
		keyHandler.put(KeyStroke.getPressed('C', 0x1, SWT.CTRL),
				getActionRegistry().getAction(ActionFactory.COPY.getId()));
		keyHandler.put(KeyStroke.getPressed('v', 0x1, SWT.CTRL),
				getActionRegistry().getAction(ActionFactory.PASTE.getId()));
		keyHandler.put(KeyStroke.getPressed('V', 0x1, SWT.CTRL),
				getActionRegistry().getAction(ActionFactory.PASTE.getId()));

	}

	protected void initializeGraphicalViewer() {
		super.initializeGraphicalViewer();
		GraphicalViewer viewer = getGraphicalViewer();

		// set the contents of this editor
		viewer.setContents(getModel());

		// add the ShortestPathConnectionRouter
		ScalableFreeformRootEditPart root = (ScalableFreeformRootEditPart) viewer
				.getRootEditPart();
		ConnectionLayer connLayer = (ConnectionLayer) root
				.getLayer(LayerConstants.CONNECTION_LAYER);

		AbstractRouter router;
		// Simple direct Line from one object to the other.
		router = new BendpointConnectionRouter();

		// Connection containing only horizontal and vertical lines.
		// router = new ManhattanConnectionRouter();

		// Rather complex path router.
		// router = new ShortestPathRouter();
		// GraphicalEditPart contentEditPart =
		// (GraphicalEditPart)root.getContents();
		// contentEditPart.getContentPane().addLayoutListener(router.getLayoutListener());

		connLayer.setConnectionRouter(router);
	}

	protected PaletteViewerProvider createPaletteViewerProvider() {
		return new PaletteViewerProvider(getEditDomain()) {
			protected void configurePaletteViewer(PaletteViewer viewer) {
				super.configurePaletteViewer(viewer);
				// create a drag source listener for this palette viewer
				// together with an appropriate transfer drop target listener,
				// this will enable
				// model element creation by dragging a
				// CombinatedTemplateCreationEntries
				// from the palette into the editor
				// @see ShapesEditor#createTransferDropTargetListener()
				viewer.addDragSourceListener(new TemplateTransferDragSourceListener(
						viewer));
			}
		};
	}

	@Override
	protected PaletteRoot getPaletteRoot() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void doSave(IProgressMonitor monitor) {
		getCommandStack().markSaveLocation();
	}
}
